package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.Calendar;

import com.dotstone.chipism.R;
import com.dotstone.chipism.timepicker.AbstractWheelTextAdapter;
import com.dotstone.chipism.timepicker.OnWheelChangedListener;
import com.dotstone.chipism.timepicker.OnWheelScrollListener;
import com.dotstone.chipism.timepicker.WheelView;
import com.dotstone.chipism.view.BlurBehind;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TimeSettingActivity2 extends BaseActivity {
	private TextView tv_statusBar_mian;
	private LinearLayout mBackL;
	private Button mNextB;
	private Button mCancelB;
	private WheelView wvYear;
	private WheelView wvMonth;
	private WheelView wvDay;
	private String selectYear;
	private String selectMonth;
	private String selectDay;
	private ArrayList<String> arry_years = new ArrayList<String>();
	private ArrayList<String> arry_months = new ArrayList<String>();
	private ArrayList<String> arry_days = new ArrayList<String>();
	private CalendarTextAdapter mYearAdapter;
	private CalendarTextAdapter mMonthAdapter;
	private CalendarTextAdapter mDaydapter;
	private boolean issetdata = false;
	private int currentYear = getYear();
	private int currentMonth = 1;
	private int currentDay = 1;
	private int maxTextSize = 24;
	private int minTextSize = 24;
	private int month;
	private int day;
	private int hour;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nextstep:
			Intent intent = new Intent();
			intent.putExtra("now", false);
			intent.putExtra("cancel", false);
			intent.putExtra("year", selectYear);
			intent.putExtra("month", selectMonth);
			intent.putExtra("day", selectDay);
			// 设置结果，并进行传送
			setResult(0, intent);
			finish();
			break;
		case R.id.cancel_btn:
			Intent intent2 = new Intent();
			intent2.putExtra("now", false);
			intent2.putExtra("cancel", true);
			setResult(0,intent2);
			finish();
			break;
		case R.id.back_btn:
			Intent intent1 = new Intent();
			intent1.putExtra("now", true);
			intent1.putExtra("cancel", false);
			setResult(0, intent1);
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			intent.putExtra("cancel", true);
			setResult(0, intent);
			finish();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_time_setting2;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		BlurBehind.getInstance().setBackground(this);
		tv_statusBar_mian = $(R.id.tv_statusBar_mian);
		mBackL = $(R.id.back_layout);
		mBackL.setVisibility(View.GONE);
		mNextB = $(R.id.nextstep);
		mNextB.setText("确定");
		mNextB.setVisibility(View.VISIBLE);
		mCancelB = $(R.id.cancel_btn);
		boolVersion();
		wvYear = $(R.id.wv_birth_year);
		wvMonth = $(R.id.wv_birth_month);
		wvDay = $(R.id.wv_birth_day);
		mCancelB.setOnClickListener(this);
		mNextB.setOnClickListener(this);
		if (!issetdata) {
			initData();
		}
		initYears();
		mYearAdapter = new CalendarTextAdapter(getApplicationContext(),
				arry_years, setYear(currentYear), maxTextSize, minTextSize);
		wvYear.setVisibleItems(4);
		// wvYear.set
		wvYear.setViewAdapter(mYearAdapter);
		wvYear.setCurrentItem(setYear(currentYear));

		initMonths(month);
		mMonthAdapter = new CalendarTextAdapter(getApplicationContext(),
				arry_months, setMonth(currentMonth), maxTextSize, minTextSize);
		wvMonth.setVisibleItems(4);
		wvMonth.setViewAdapter(mMonthAdapter);
		wvMonth.setCurrentItem(setMonth(currentMonth));
		Log.i("wmy", 142 + "initDays" + this.day);
		// initDays(day);
		initDays1(getMonth());

		mDaydapter = new CalendarTextAdapter(getApplicationContext(),
				arry_days, currentDay - 1, maxTextSize, minTextSize);
		wvDay.setVisibleItems(4);
		wvDay.setViewAdapter(mDaydapter);
		wvDay.setCurrentItem(currentDay - 1);
		// initHours(hour);
		// initHours1(getDay());

		wvYear.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mYearAdapter.getItemText(wheel
						.getCurrentItem());
				selectYear = currentText;
				setTextviewSize(currentText, mYearAdapter);
				currentYear = Integer.parseInt(currentText);
				setYear(currentYear);
				// initMonths(month);
				mMonthAdapter = new CalendarTextAdapter(
						getApplicationContext(), arry_months, 0, maxTextSize,
						minTextSize);
				wvMonth.setVisibleItems(5);
				wvMonth.setViewAdapter(mMonthAdapter);
				wvMonth.setCurrentItem(0);
			}
		});

		wvYear.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mYearAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mYearAdapter);
			}
		});

		wvMonth.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mMonthAdapter.getItemText(wheel
						.getCurrentItem());
				selectMonth = currentText;
				setTextviewSize(currentText, mMonthAdapter);
				setMonth(Integer.parseInt(currentText));
				Log.i("wmy", 205 + "initDays" + day);
				// initDays(day);
				initDays1(Integer.parseInt(selectMonth));
				mDaydapter = new CalendarTextAdapter(getApplicationContext(),
						arry_days, 0, maxTextSize, minTextSize);
				wvDay.setVisibleItems(5);
				wvDay.setViewAdapter(mDaydapter);
				wvDay.setCurrentItem(0);
			}
		});

		wvMonth.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mMonthAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mMonthAdapter);
			}
		});

		wvDay.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mDaydapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mDaydapter);
				selectDay = currentText;
				// initHours1(day);
			}
		});

		wvDay.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mDaydapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mDaydapter);
			}
		});

	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) tv_statusBar_mian
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			tv_statusBar_mian.setLayoutParams(layoutParams1);
			tv_statusBar_mian.setVisibility(View.VISIBLE);

		} else {
			tv_statusBar_mian.setVisibility(View.GONE);
		}
	}

	public void initData() {
		setDate(getYear(), getMonth(), getDay(), getHour());
		this.currentDay = 1;
		this.currentMonth = 1;
	}

	public int getYear() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR);
	}

	public int getMonth() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.MONTH) + 1;
	}

	public int getDay() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.DATE);
	}

	public int getHour() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.HOUR_OF_DAY);
	}

	public void setDate(int year, int month, int day, int hour) {
		selectYear = year + "";
		selectMonth = month + "";
		selectDay = day + "";
		issetdata = true;
		this.currentYear = year;
		this.currentMonth = month;
		this.currentDay = day;
		if (year == getYear()) {
			this.month = getMonth();
		} else {
			this.month = 12;
		}
		calDays(year, month, day);
	}

	public void calDays(int year, int month, int day) {
		boolean leayyear = false;
		if (year % 4 == 0 && year % 100 != 0) {
			leayyear = true;
		} else {
			leayyear = false;
		}
		// for (int i = 1; i <= 12; i++) {
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			this.day = 31;
			break;
		case 2:
			if (leayyear) {
				this.day = 29;
			} else {
				this.day = 28;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			this.day = 30;
			break;
		}
		// mDaydapter.notify();
		if (year == getYear() && month == getMonth()) {
			Log.i("wmy", "第" + month + "个月份 共" + this.day + "天" + "getDay() = "
					+ getDay());
			this.day = this.day - getDay();
			Log.i("wmy", "this.day = " + this.day);
		}
		this.hour = 24;
	}

	private void initDays1(int month) {
		arry_days.clear();
		Log.i("wmy", "传入月份" + month);
		boolean leayyear = false;
		if (Integer.parseInt(selectYear) % 4 == 0
				&& Integer.parseInt(selectYear) % 100 != 0) {
			leayyear = true;
		} else {
			leayyear = false;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			this.day = 31;
			break;
		case 2:
			if (leayyear) {
				this.day = 29;
			} else {
				this.day = 28;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			this.day = 30;
			break;
		}
		if (selectYear.equals(getYear() + "")
				&& selectMonth.equals(getMonth() + "")) {
			Log.i("wmy", "同年同月");
			// this.day = this.day - getDay();
			for (int i = getDay(); i <= this.day; i++) {
				arry_days.add(i + "");
			}
		} else {
			Log.i("wmy", "不同年同月");
			for (int i = 1; i <= this.day; i++) {
				arry_days.add(i + "");
			}
		}
	}

	public void initYears() {
		for (int i = getYear(); i < (getYear() + 5); i++) {
			arry_years.add(i + "");
		}
	}

	public void initMonths(int months) {
		arry_months.clear();
		// for (int i = 1; i <= months; i++) {
		// arry_months.add(i + "");
		// }
		for (int i = getMonth(); i <= 12; i++) {
			arry_months.add(i + "");
		}
	}

	// public void initDays(int days) {
	// arry_days.clear();
	// Log.i("wmy", "初始化天数" + days);
	// if (selectMonth.equals(getMonth() + "")) {
	// for (int i = 1; i <= days; i++) {
	// arry_days.add(i + "");
	// }
	// } else {
	// for (int i = 1; i <= days; i++) {
	// arry_days.add(i + "");
	// }
	// }
	// }

	// public void initHours(int hours) {
	// arrr_hours.clear();
	// for (int i = 1; i <= hours; i++) {
	// arrr_hours.add(i + "");
	// }
	// }

	// public void initHours1(int day) {
	// arrr_hours.clear();
	// if (day == getDay()) {
	// for (int i = getHour() + 1; i <= 24; i++) {
	// arrr_hours.add(i + "");
	// }
	// } else {
	// for (int i = 1; i <= 24; i++) {
	// arrr_hours.add(i + "");
	// }
	// }
	// }

	private class CalendarTextAdapter extends AbstractWheelTextAdapter {
		ArrayList<String> list;

		protected CalendarTextAdapter(Context context, ArrayList<String> list,
				int currentItem, int maxsize, int minsize) {
			super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem,
					maxsize, minsize,0);
			this.list = list;
			setItemTextResource(R.id.tempValue);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return list.get(index) + "";
		}
	}

	public int setYear(int year) {
		int yearIndex = 0;
		if (year != getYear()) {
			this.month = 12;
		} else {
			this.month = getMonth();
		}
		for (int i = getYear(); i > 1950; i--) {
			if (i == year) {
				return yearIndex;
			}
			yearIndex++;
		}
		return yearIndex;
	}

	public int setMonth(int month) {
		int monthIndex = 0;
		calDays(currentYear, month, day);
		for (int i = 1; i < this.month; i++) {
			if (month == i) {
				return monthIndex;
			} else {
				monthIndex++;
			}
		}
		return monthIndex;
	}

	public void setTextviewSize(String curriteItemText,
			CalendarTextAdapter adapter) {
		ArrayList<View> arrayList = adapter.getTestViews();
		int size = arrayList.size();
		String currentText;
		for (int i = 0; i < size; i++) {
			TextView textvew = (TextView) arrayList.get(i);
			currentText = textvew.getText().toString();
			if (curriteItemText.equals(currentText)) {
				textvew.setTextSize(maxTextSize);
				textvew.setTextColor(Color.parseColor("#ffffff"));
			} else {
				textvew.setTextSize(minTextSize);
				textvew.setTextColor(Color.parseColor("#4dffffff"));
			}
		}

	}

}